package com.qxsx.midi.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.MeMsgListAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.model.Msg;
import com.qxsx.midi.model.Order;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.ZListView.ZListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class MeMsgListActivity extends BaseActivity {
    private CustomProgress customProgress;


    ZListView listView;
    private ArrayList<Msg> allList = new ArrayList<>();
    private ArrayList<Msg> lists;
    private MeMsgListAdapter listAdapter;
    private int _page = 1; //   初始化页码为1
    private static final int PAGESIZE = 10; //每页请求数量
    private Handler mHandler;  //执行下拉、上拉线程
    private String _userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_msg_list);
        setTitle("我的消息");
        setNavBtn(R.mipmap.back, "", 0, "清空");
        findById();
        inItData();
        mHandler = new Handler();


    }

    private void inItData() {
        _userId = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        lists = new ArrayList<>();

        listAdapter = new MeMsgListAdapter(this, lists,MeMsgListActivity.this);      //
        listView.setAdapter(listAdapter);
        allList.clear();
        listAdapter.RemoveAll();
        memsg(_page);

        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);

        listView.setXListViewListener(new ZListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        _page = 1;
                        allList.clear();
                        listAdapter.RemoveAll();
                        memsg(_page);

                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        _page++;
                        memsg(_page);
                    }
                }, 2000);
            }
        });


    }

    private void findById() {
        listView = (ZListView) findViewById(R.id.msg_list);


    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

            case RIGHT:
                popWindow(v);
                break;

        }
    }

    private void delete() {
        customProgress = CustomProgress.show(MeMsgListActivity.this, "正在删除....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        try {
            object.put("user_id", user_id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("112", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {

                listAdapter.RemoveAll();
                customProgress.dismiss();

            }
        });
    }


    private void memsg(int page) {
        customProgress = CustomProgress.show(MeMsgListActivity.this, "请稍后....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            object.put("user_id", _userId);
            object.put("nowpage", page + "");
            object.put("pagesize", PAGESIZE + "");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("110", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if ("200".equals(code.toString())) {

                    try {
                        lists = new ArrayList<>();
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object = array.getJSONObject(i);
                            Msg msg = new Msg();
                            msg.setId(object.getString("id"));
                            msg.setTitle(object.getString("title"));
                            msg.setDate(object.getString("message_time"));
                            msg.setContent(object.getString("message_cotent"));
                            lists.add(msg);
                            allList.add(msg);
                        }
                        listAdapter.AddData(lists);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                }
                listView.stopRefresh();
                listView.stopLoadMore();
                customProgress.dismiss();
            }
        });

    }

    PopupWindow popupWindow;

    public void popWindow(View v) {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_me_msg_rm_diag, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置

        // 拨打电话
        Button cancel = (Button) menuView.findViewById(R.id.btn_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        // 拨打电话
        Button call = (Button) menuView.findViewById(R.id.btn_ok);
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (allList.size() == 0) {
                    ShowToast("没有要清空的消息");
                } else {
                    delete();
                }
                popupWindow.dismiss();
            }
        });
    }

}
